package com.wkhyapp.lm.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidong.photopicker.intent.PhotoPreviewIntent2;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GoodsImageAdapter;
import com.wkhyapp.lm.app.WkhyApp;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.constant.Constant;
import com.wkhyapp.lm.contract.GoodsPresenter;
import com.wkhyapp.lm.contract.GoodsView;
import com.wkhyapp.lm.fragment.PagesFragment;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.ImageLoadUtil;
import com.wkhyapp.lm.utils.ImgDonwloads;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GoodsInfoActivity extends SuperActivity<GoodsPresenter> implements GoodsView {

    MZBannerView mzBanner;
    private Long goodsId;
    @BindView(R.id.img_lv)
    ListView img_lv;
    private GoodsImageAdapter goodsImageAdapter;
    private List<String> list;
    private View headView;
    private TextView goods_title,rd_tv;
    private TextView goodsid_tv;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.sc_iv)
    ImageView sc_iv;
    @BindView(R.id.fenxiang_rl)
    RelativeLayout fenxiang_rl;
    @BindView(R.id.shangjai_rl)
    RelativeLayout shangjai_rl;
    @BindView(R.id.changtu_rl)
    RelativeLayout changtu_rl;

    private TextView price_tv;
    Goods goods;


    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        goodsId = getIntent().getLongExtra("0",0L);

        headView = View.inflate(mContext, R.layout.head_goods_info, null);
        headView.findViewById(R.id.back_iv);
        headView.findViewById(R.id.sc_iv);
        goodsid_tv = (TextView) headView.findViewById(R.id.goodsid_tv);
        mzBanner = (MZBannerView) headView.findViewById(R.id.mzBanner);
        goods_title =(TextView) headView.findViewById(R.id.goods_title);
        price_tv =(TextView) headView.findViewById(R.id.price_tv);
        rd_tv =(TextView) headView.findViewById(R.id.rd_tv);
        list = new ArrayList<>();
        img_lv.addHeaderView(headView);
        goodsImageAdapter = new GoodsImageAdapter(mContext,list,R.layout.item_goods_img);
        mPresenter.getData(goodsId);
        showLoad("");
    }



    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        sc_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MemberUtils.isLogin()){
                    mPresenter.gz(goodsId);
                }else{
                    TToast("请先登录...");
                    goTo(LoginActivity.class);
                }

            }
        });

        fenxiang_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(ShareGoodsActivity.class,goodsId);
            }
        });

        shangjai_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(ShopActivity.class,goods.getUserId());
            }
        });

        mzBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener(){
            @Override
            public void onPageClick(View view, int i) {
                PhotoPreviewIntent2 intent = new PhotoPreviewIntent2(mContext);
                intent.setCurrentItem(i);
                intent.setPhotoPaths(goods.getGoodsImgs());
                startActivityForResult(intent, 1);

            }
        });

        goodsImageAdapter.setCallBack(new GoodsImageAdapter.callBack() {
            @Override
            public void view(String item) {
                PhotoPreviewIntent2 intent = new PhotoPreviewIntent2(mContext);
                intent.setCurrentItem(0);
                intent.setPhotoPaths(goods.getGoodsImgs());
                startActivityForResult(intent, 1);
            }
        });

        changtu_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TToast("长图");
                try {
                    ImgDonwloads.saveFile(mContext, getListViewBitmap(img_lv, Constant.imagFilePatch),"");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public void setGoods(final Goods goods) {
        this.goods = goods;
        dismissLoad();
        list.addAll(goods.getGoodsImgs());
        img_lv.setAdapter(goodsImageAdapter);
        goodsImageAdapter.notifyDataSetChanged();
        goods_title.setText(goods.getDesc());
        goodsid_tv.setText("商品编号: "+goods.getId());
        rd_tv.setText("热度 "+goods.getViewed());
        if(!StringUtils.isEmpty(goods.getPrice())){
            price_tv.setText(goods.getPrice());
        }

        if(goods.isFollow()){
            sc_iv.setBackgroundResource(R.drawable.collected);
        }else{
            sc_iv.setBackgroundResource(R.drawable.collect);
        }

        mzBanner.setPages(goods.getGoodsImgs(), new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });


        if(goods!=null){
            if(goods.getGoodsImgs().size()>=1){
                mzBanner.start();
            }
        }
    }

    @Override
    public void setSucc(String msg) {
        mPresenter.getData(goodsId);
    }

    @Override
    protected GoodsPresenter createPresenter() {
        return mPresenter = new GoodsPresenter(this);
    }


    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_info;
    }


    public static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_goods,null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }
        @Override
        public void onBind(Context context, int position, String data) {
            ImageLoadUtil.setRoundImage_Normal(WkhyApp.getAppContext(),data,0,mImageView);
        }
    }


    /**
     * 截图listview
     **/
    public static Bitmap getListViewBitmap(ListView listView, String picpath) {
        int h = 0;
        Bitmap bitmap;
        // 获取listView实际高度
        for (int i = 1; i < listView.getChildCount(); i++) {
            h += listView.getChildAt(i).getHeight();
        }
        listView.getHeight();
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(listView.getWidth(), h,
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        listView.draw(canvas);
        return bitmap;
    }

}
